package com.beiding.dao.impl;

import com.beiding.dao.SettingsDao;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class SettingsDaoImpl implements SettingsDao {


    private HashOperations<String, String, String> settingTable;

    public SettingsDaoImpl(RedisTemplate<String, String> redisTemplate) {
        this.settingTable = redisTemplate.opsForHash();
    }

    //获取平台费率
    @Cacheable(cacheNames = "order", key = "'platformCostRate'")
    @Override
    public Double getPlatformCostRate() {
        String r = get("platformCostRate");

        return r == null ? null : Double.valueOf(r);
    }

    //获取超时时间
    @Cacheable(cacheNames = "order", key = "#item")
    @Override
    public Long getTimeout(String item) {
        String s = get(item);
        return s == null ? null : Long.valueOf(s);

    }

    @Override
    @CacheEvict(cacheNames = "order", key = "'platformCostRate'")
    public void setPlatformCostRate(Double v) {
        set("platformCostRate", String.valueOf(v));
    }

    @Override
    @CacheEvict(cacheNames = "order", key = "#item")
    public void setTimeout(String item, Long l) {
        set(item, String.valueOf(l));
    }


    private String get(String item) {
        return settingTable.get("OrderSettingTable", item);
    }

    private void set(String item, String value) {
        settingTable.put("OrderSettingTable", item, value);
    }



}
